interface ListQuerysFace {
  page: number;
  size: number;
}
let timer: number;

import storage from "@/utils/storage";
class chatSql {
  maxLength: number;
  list: ChatFace[];
  newlist: ChatFace[];
  constructor() {
    this.maxLength = 1000;
    let list = (storage.get("chatList") as ChatFace[]) || [];
    this.list = list.reverse();
    this.newlist = [...list];
  }
  pagination(pageNo: number, pageSize: number, array: ChatFace[]) {
    var offset = (pageNo - 1) * pageSize;
    var data = array.slice(offset, offset + pageSize);
//    data.reverse();
    return data;
  }
  getList(listQuery: ListQuerysFace): Promise<IResponseList<ChatFace[]>> {
    return new Promise((resolve, reject) => {
      const { page, size } = listQuery;
      let detail = this.pagination(page, size, this.list);
      detail= detail.reverse()
      resolve({
        code: 200,
        data: {
          data: detail,
          count: this.list.length,
        },
      });
    });
  }
  update(){
    clearTimeout(timer);
    timer = setTimeout(() => {
        storage.set("chatList", this.newlist);
      }, 1000);
  }
  addHistory = (msg: string, type: any,addData={}) => {
    clearTimeout(timer);
    const obj: ChatFace = {
      msg,
      id: this.newlist.length,
      type,
      msgType: 1,
      status: 1,
      userId: "", // type === 2 ? userStore.getUser.member_id : undefined,
      ismy: type === 2,
      ...addData
    };

    this.newlist.push(obj);
    timer = setTimeout(() => {
      storage.set("chatList", this.newlist);
    }, 1000);

    return obj;
  };
}
const sqls = new chatSql();
export default sqls;
